import matplotlib.pyplot as plt
import numpy as np


import matplotlib.pyplot as plt
import numpy as np


# 时间间隔和比特率数据
intervals = np.arange(0, 60)

# AllReduce 的比特率数据（Gbits/sec）
bitrate_allreduce = [
    18.4, 12.5, 14.5, 13.3, 13.6, 14.9, 14.6, 12.0, 14.3, 15.5,
    15.4, 15.0, 13.7, 11.9, 12.2, 15.3, 13.9, 13.4, 13.0, 12.7,
    13.3, 13.8, 14.7, 13.8, 14.2, 14.3, 10.6, 16.4, 13.1, 15.1,
    17.1, 12.3, 14.9, 13.9, 14.9, 17.1, 14.3, 14.9, 15.1, 16.3,
    15.5, 14.1, 16.8, 13.2, 17.6, 12.3, 15.8, 12.0, 13.9, 12.3,
    14.3, 12.2, 13.7, 12.8, 13.2, 12.3, 13.5, 15.4, 12.7, 15.4
]

# NetSenseML 的比特率数据（Gbits/sec）
bitrate_netsenseml = [
    24.6, 32.3, 21.7, 40.6, 19.6, 27.1, 26.3, 27.1, 29.4, 20.3,
    17.4, 36.5, 10.1, 35.3, 12.0, 27.6, 11.5, 33.9, 9.42, 29.0,
    11.9, 33.6, 8.30, 34.7, 10.4, 29.8, 10.4, 28.0, 9.21, 27.8,
    14.6, 24.7, 24.3, 38.7, 10.4, 28.4, 12.7, 29.0, 9.36, 31.1,
    10.7, 23.1, 34.3, 13.3, 29.9, 12.6, 31.2, 13.8, 30.5, 13.9,
    26.5, 12.9, 32.4, 12.8, 30.1, 10.7, 32.2, 11.8, 26.5, 13.2
]

# TopK 的比特率数据（Gbits/sec）
bitrate_topk = [
    33.5, 16.3, 17.6, 21.9, 10.8, 23.0, 18.3, 16.3, 22.2, 30.2,
    17.8, 18.2, 15.9, 18.1, 23.5, 15.4, 18.7, 19.3, 20.1, 19.4,
    20.5, 17.3, 13.9, 16.1, 17.1, 15.5, 30.8, 14.9, 18.2, 26.1,
    21.4, 18.8, 19.6, 21.0, 18.6, 7.16, 19.4, 16.4, 23.2, 15.8,
    17.9, 19.7, 21.5, 15.8, 22.4, 15.6, 25.6, 13.0, 23.6, 17.6,
    17.8, 17.1, 18.8, 15.6, 23.1, 14.9, 15.5, 16.7, 25.5, 13.8
]

fig, ax = plt.subplots(figsize=(10, 6))

# 定义不同颜色和样式
ax.plot(intervals, bitrate_allreduce, label="AllReduce", marker='o', linestyle='-', color='skyblue', linewidth=2)
ax.plot(intervals, bitrate_netsenseml, label="NetSenseML", marker='*', linestyle='--', color='orange', linewidth=2)
ax.plot(intervals, bitrate_topk, label="TopK", marker='s', linestyle='-.', color='green', linewidth=2)

# 设置标题和标签的字体大小
ax.set_xlabel('Time (seconds)', fontsize=14, fontweight='bold')
ax.set_ylabel('Bitrate (Gbits/sec)', fontsize=14, fontweight='bold')
ax.set_title('Bandwidth Comparison: AllReduce vs NetSenseML vs TopK', fontsize=16, fontweight='bold')

# 调整图例大小和位置
ax.legend(fontsize=12, loc='upper right')

# 增加网格线，并设置线条透明度
ax.grid(True, which='both', linestyle='--', alpha=0.7)

# 优化图表布局
plt.tight_layout()

import matplotlib.pyplot as plt
import numpy as np

# 数据
methods = ['AllReduce', 'TopK', 'NetSenseML']
bandwidth_used = [14.2, 19.0, 25.0]  # Gbits/sec
total_bandwidth = 50.0  # 总带宽为 50 Gbits/sec

# 创建图表
fig, ax = plt.subplots(figsize=(8, 6))

# 绘制方法的带宽使用柱状图
bars = ax.bar(methods, bandwidth_used, color=['skyblue', 'green', 'orange'], label='Bandwidth Used (Gbits/sec)')

# 添加总带宽的水平线
ax.axhline(y=total_bandwidth, color='red', linestyle='--', linewidth=2, label=f'Total Bandwidth ({total_bandwidth} Gbits/sec)')

# 添加标签和标题
ax.set_xlabel('Methods', fontsize=14, fontweight='bold')
ax.set_ylabel('Bandwidth (Gbits/sec)', fontsize=14, fontweight='bold')
ax.set_title('Bandwidth Usage vs Total Bandwidth for Different Methods', fontsize=16, fontweight='bold')

# 显示具体带宽标签
for bar in bars:
    height = bar.get_height()
    ax.annotate(f'{height:.1f} Gbps', xy=(bar.get_x() + bar.get_width() / 2, height),
                xytext=(0, 3), textcoords="offset points", ha='center', fontsize=12)

# 显示图例
ax.legend()

# 设置网格线
ax.grid(True, linestyle='--', alpha=0.7)

# 优化图表布局
plt.tight_layout()

# 显示图表
plt.show()